// packageActivity/activity/activity.js
import { getEstablishInfo,beInvitedInfo } from "../../api/activity"
import {
    getEnterpriseList
} from '../../../api/app.js'
import { toast,confirm } from "../../../utils/util"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabIndex:0,
        qrId:'',
        info:{},
        enterprise:{
            name:'',
            code:'',
            phone:'',
            userName:'',
            area:'',
            id:''
        },
        isInfoPopup:false,
        toView:'',
        bodyTop:''
    },

    onLoad(options){
        console.log('进入啦活动页面',options)
        this.getRect()
        let scene = decodeURIComponent(options.scene).split('=')[1]
        this.setData({
            qrId:scene
        })
        this.getEstablishInfo()
        console.log(this.data.qrId,'scene')
    },
    onRuleTabs(){
        this.setData({
            tabIndex:2,
            toView:'ronus'
        })
    },
    onRonusTabs(){
        this.setData({
            tabIndex:0,
        })
    },
    onProgrammeTabs(){
        this.setData({
            tabIndex:1,
            toView:'programme'
        })
    },
    getEstablishInfo(){
        getEstablishInfo({
            qrCodeId:this.data.qrId
        }).then(res=>{
            console.log(res,'res')
            this.setData({
                info:res
            })
        })
    },
    onEnterpriseInput(event){
        const { value } = event.detail
        getEnterpriseList({
            name:value
        }).then(res=>{
            this.setData({
                tipList:res
            })
        })
    },
    onTapEnter(event){
        const { name,code,area } = event.currentTarget.dataset
        this.setData({
            ['enterprise.name']:name,
            ['enterprise.code']:code,
            ['enterprise.area']:area,
            tipList:[],
            isSendOutBtn:true
        })
        console.log(this.data.isSendOutBtn,'isSendOutBtn')
    },
    onNameInput(event){
        const { value } = event.detail
        this.setData({
            ['enterprise.userName']:value
        })
    },
    onPhoneInput(event){
        const { value } = event.detail
        this.setData({
            ['enterprise.phone']:value
        })
    },
    onPreciseExtension(){
        if(!this.data.enterprise.userName){
            toast('请填写姓名')
            return
        }
        if(!this.data.enterprise.phone){
            toast('请填写手机号')
            return
        }
        if(!this.data.enterprise.code){
            toast('请选择公司')
            return
        }
        console.log(this.data.enterprise)
        beInvitedInfo({
            contactName:this.data.enterprise.userName,
            contactPhone:this.data.enterprise.phone,
            customName:this.data.enterprise.name,
            creditCode:this.data.enterprise.code,
            area:this.data.enterprise.area,
            qrcodeId:this.data.qrId
        }).then(async res=>{
            console.log(res.id || res,'res')
            this.setData({
                ['enterprise.id']:res.id || ''
            })
            await this.cancel()
            console.log(this.data.enterprise)
            wx.navigateTo({
                url: '/packageActivity/pages/success/success',
                success:(res)=>{
                    res.eventChannel.emit('acceptDataFromOpenerPage', { data: this.data.enterprise,type:'manyPeople' })
                    this.cancel()
                }
            })
        })
    },
    cancel(){
        this.setData({
            isInfoPopup:false
        })
    },
    onImmediateUse(){
        this.setData({
            isInfoPopup:true
        })
    },
    getRect () {
        console.log('调用啦')
        wx.createSelectorQuery().select('#the-id').boundingClientRect((rect=>{
            console.log(rect.top)
            this.setData({
                bodyTop:rect.top
            })
            console.log(this.data.bodyTop,'bodyTop')
        })).exec()
        
      },
})